package thong.servlet;

import java.io.IOException;
import java.util.SortedMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.sql.Result;

import thong.util.JdbcUtil;

@WebServlet("/category.do")
public class CategoryCRUDServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		/*
		 *  nhận tham số từ form nhập
		 */
		String id = request.getParameter("txtId");
		String name = request.getParameter("txtName");
        String image = request.getParameter("txtImage");
        String description = request.getParameter("txtDescription");
        
        /* 
         * Làm việc với CSDL khi tồn tài các tham số điều khiển tương 
         * ứng (btnInsert, btnUpdate, btnDelete, btnSelect, btnSearch)
         */
        try{
	        if(request.getParameter("btnInsert") != null){
		        String sql = "INSERT INTO Categories(Name, Image, Description) VALUES(?, ?, ?)";
		        JdbcUtil.executeUpdate(sql, name, image, description);
		        request.setAttribute("errors", "Inserted successfully !");
	        }
	        else if(request.getParameter("btnUpdate") != null){
	        	String sql = "UPDATE Categories SET Name=?, Image=?, Description=? WHERE Id=?";
		        JdbcUtil.executeUpdate(sql, name, image, description, id);
		        request.setAttribute("errors", "Updated successfully !");
	        }
	        else if(request.getParameter("btnDelete") != null){
		        String sql = "DELETE FROM Categories WHERE Id=?";
		        JdbcUtil.executeUpdate(sql, id);
		        request.setAttribute("errors", "Deleted successfully !");
	        }
	        else if(request.getParameter("lnkEdit") != null){
		        String sql = "SELECT * FROM Categories WHERE Id=?";
		        /*
		         * Truy vấn và chia sẻ kết quả trong request
		         */
		        Result result = JdbcUtil.toResult(sql, id);
		        @SuppressWarnings("rawtypes")
				SortedMap category = result.getRows()[0];
		        request.setAttribute("cate", category);
		        request.setAttribute("errors", "Loaded successfully !");
	        }
        }
        catch (Exception e) {
        	request.setAttribute("errors", "Data manipilation error !");
		}
        /*
         * Duy trì chuỗi tìm kiếm của câu lệnh sql trong session
         */
        HttpSession session = request.getSession();
        if(request.getParameter("btnSearch") != null){
        	session.setAttribute("search", request.getParameter("txtSearch"));
        }
        else if(session.getAttribute("search") == null){
        	session.setAttribute("search", "");
        }
        String search = (String)session.getAttribute("search");
        
        /*
         * Tìm kiếm theo tên và chia sẽ kết quả trong request
         */
        String searchSql = "SELECT * FROM Categories  WHERE Name LIKE ?";
        Result categories = JdbcUtil.toResult(searchSql, "%" + search + "%");
        request.setAttribute("cates", categories);
        /*
         * Chuyển tiếp sang trang CategoryCRUD.jsp
         */
        request.getRequestDispatcher("pages/ManagerCategory.jsp").forward(request, response);
	}
}
